package tool;

import cn.hutool.core.util.ObjectUtil;
import constant.ConfigConstants;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/6
 */
public class Stature {

    public static void openStatureStage() {
        Stage stature = new Stage();
        stature.setTitle("身高体重计算");
        stature.setFullScreen(ConfigConstants.FullScreen);

        // 创建下拉框
        ComboBox<String> genderComboBox = new ComboBox<>();
        genderComboBox.getItems().addAll("男", "女");
        genderComboBox.setPromptText("选择性别");
        // 创建身高输入框
        TextField heightTextField = new TextField();
        heightTextField.setPromptText("输入身高（厘米）");
        // 创建体重输入框
        TextField weightTextField = new TextField();
        weightTextField.setPromptText("输入体重（千克）");
        Button calculateButton = new Button("测算");
        // 创建一个WebView组件
        WebView webView = new WebView();
        webView.setPrefSize(500, 650);
        calculateButton.setOnAction(event -> {
            String gender = genderComboBox.getValue();
            String height = heightTextField.getText();
            String weight = weightTextField.getText();
            if (gender == null || gender.isEmpty() || height.isEmpty() || weight.isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("警告");
                alert.setHeaderText(null);
                alert.setContentText("请输入性别、身高和体重值");
                alert.showAndWait();
            } else {
                String result = calculateStature(gender, height, weight);
                webView.getEngine().loadContent(result);
            }
        });
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(genderComboBox, heightTextField, weightTextField, calculateButton, webView);
        Scene scene = new Scene(root, 500, 700);
        stature.setScene(scene);
        stature.show();
    }

    private static String calculateStature(String gender, String height, String weight) {
        if (ObjectUtil.isEmpty(gender))
            return null;
        if (ObjectUtil.isEmpty(height))
            return null;
        if (ObjectUtil.isEmpty(weight))
            return null;
        // 标准体重
        double standardWeight = calculateStandardWeight(Double.parseDouble(height), gender);
        // 判断是否是标准体重
        boolean isStandardWeight = isStandardWeight(Double.parseDouble(weight), Double.parseDouble(height), gender);
        // 计算BMI指数
        double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
        DecimalFormat df = new DecimalFormat("#.#");
        df.setRoundingMode(RoundingMode.HALF_EVEN);
        String roundedBmi = df.format(bmi * 10000);
        // 计算范围
        String bmiStr = calculateWeightStatus(bmi * 10000);
        StringBuilder sb = new StringBuilder();
        sb.append("<div style=\"background-color: #f2f2f2; padding: 10px;\">")
                .append("<h3>标准身材测算：</h3>")
                .append("<div>")
                .append("<ul style=\"list-style-type: none; padding: 0;\">");
        sb.append("<li style=\"list-style-type: none; padding: 0;\">")
                .append("<strong>标准体重：</strong>").append((int) Math.round(standardWeight)).append("<br/>")
                .append("<strong>是否是标准体重：</strong>").append(isStandardWeight ? "是" : "否").append("<br/>")
                .append("<strong>BMI指数：</strong>").append(roundedBmi).append("<br/>")
                .append("<strong>BMI结果：</strong>").append(bmiStr).append("<br/>")
                .append("</li>");
        return sb.toString();
    }

    public static double calculateStandardWeight(double height, String gender) {
        double standardWeight;
        if (gender.equals("男")) {
            standardWeight = 0.7 * (height - 80);
        } else if (gender.equals("女")) {
            standardWeight = 0.6 * (height - 70);
        } else {
            throw new IllegalArgumentException("无效的性别");
        }
        return standardWeight;
    }

    public static boolean isStandardWeight(double weight, double height, String gender) {
        double standardWeight = calculateStandardWeight(height, gender);
        double lowerBound = standardWeight - 5;
        double upperBound = standardWeight + 5;
        return weight >= lowerBound && weight <= upperBound;
    }

    public static double calculateBMI(double weight, double height) {
        return weight / (height * height);
    }

    public static String calculateWeightStatus(double bmi) {
        if (bmi < 18.5) {
            return "体重过轻";
        } else if (bmi < 24.9) {
            return "正常范围";
        } else if (bmi < 29.9) {
            return "超重";
        } else {
            return "肥胖";
        }
    }
}
